package com.atguigu.gmall.product.init;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class InitRunner implements CommandLineRunner {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    SkuInfoService skuInfoService;

    @Override
    public void run(String... args) throws Exception {
        log.info("initRunner 组件启动，调用run（）方法，skuInfoService = " + skuInfoService );
        initBitmap();

    }

    private void initBitmap() {
        log.info("正在初始化skuid - bitmap");
        List<SkuInfo> ids = skuInfoService.lambdaQuery().select(SkuInfo::getId).list();
        ids.stream().forEach(item -> {
            redisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP,item.getId(),true);
        });
        log.info("初始化 skuid - bitmap 完成");
    }
}
